package soft.quicknote.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Spinner;
import android.widget.TextView;

import org.joda.time.LocalDateTime;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import soft.quicknote.DBHelper;
import soft.quicknote.Operation;
import soft.quicknote.QueryUtil;
import soft.quicknote.R;
import soft.quicknote.adapter.CategoryNameAdapter;
import soft.quicknote.adapter.NoteAdapter;
import soft.quicknote.beans.Category;
import soft.quicknote.beans.Note;


public class SearchActivity extends Activity {
    private static final String TAG = SearchActivity.class.getSimpleName();

    SearchView mSearchView;
    Spinner mSpinner;
    Spinner mDateSpinner;
    Spinner mCatSpinner;
    ListView searchResult;
    NoteAdapter adapter;
    ArrayList<Note> list;
    TextView noResult;
    DBHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        helper = new DBHelper(this);
        list = new ArrayList<>();
        adapter = new NoteAdapter(this, list);

        searchResult = (ListView) findViewById(R.id.search_result);
        searchResult.setAdapter(adapter);
        searchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Note note = SearchActivity.this.adapter.getItem(position);
                startEditActivity(note.getId());
            }
        });

        noResult = (TextView) findViewById(R.id.no_search_result);

        mDateSpinner = (Spinner) findViewById(R.id.date_spinner);
        mSpinner = (Spinner) findViewById(R.id.spinner_search);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapter.notifyDataSetChanged();
                adapter.clear();

                mSearchView.setVisibility(View.INVISIBLE);

                mSearchView.setQuery("", false);

                mDateSpinner.setVisibility(View.INVISIBLE);
                mDateSpinner.setSelection(0);

                mCatSpinner.setVisibility(View.INVISIBLE);
                mCatSpinner.setSelection(0);

                TextView textView = (TextView) view;
                if (textView.getText().equals("日期")) {
                    mDateSpinner.setVisibility(View.VISIBLE);
                } else if (textView.getText().equals("分类")) {
                    mCatSpinner.setVisibility(View.VISIBLE);
                } else {
                    mSearchView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mSearchView = (SearchView) findViewById(R.id.searchView);
        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                adapter.clear();
                if (query.equals("")) {
                    return true;
                }
                reset();
                String type = (String) mSpinner.getSelectedItem();
                ArrayList<Note> res = null;
                switch (type) {
                    case "标题":
                        res = QueryUtil.queryNoteFromTitle(helper, query);
                        break;
                    case "内容":
                        res = QueryUtil.queryNoteFromContent(helper, query);
                        break;
                    default:
                        res = QueryUtil.queryNoteFromContent(helper, query);
                        break;
                }
                if (res != null) adapter.addAll(res);
                adapter.notifyDataSetChanged();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        mDateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) return;
                adapter.clear();
                adapter.notifyDataSetChanged();

                TextView textView = (TextView) view;
                LocalDateTime now = LocalDateTime.now();
                ArrayList<Note> res = null;
                switch (textView.getText().toString()) {
                    case "1天内":
                        res = QueryUtil.queryNoteFromDateRange(helper, now.minusDays(1), now);
                        break;
                    case "3天内":
                        res = QueryUtil.queryNoteFromDateRange(helper, now.minusDays(3), now);
                        break;
                    case "7天内":
                        res = QueryUtil.queryNoteFromDateRange(helper, now.minusDays(7), now);
                        break;
                    case "7天外":
                        res = QueryUtil.queryNoteFromDateRange(helper, new LocalDateTime(new Timestamp(0)), now.minusDays(7));
                        break;
                }
                if (res != null) adapter.addAll(res);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        final ArrayList<Category> cats = helper.queryAllCategory();
        cats.add(0, new Category(-1, "空"));
        CategoryNameAdapter nameAdapter = new CategoryNameAdapter(this, cats);

        nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mCatSpinner = (Spinner) findViewById(R.id.category_spinner);
        mCatSpinner.setAdapter(nameAdapter);

        mCatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapter.clear();
                adapter.notifyDataSetChanged();
                if (position == 0) {
                    return;
                }

                Category cat = cats.get(position);
                ArrayList<Note> res = QueryUtil.queryNoteFromCategory(helper, cat.getId());
                adapter.addAll(res);
                adapter.notifyDataSetChanged();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    public void reset() {
        adapter.clear();
        noResult.setVisibility(View.INVISIBLE);
    }


    void startEditActivity(int noteId) {
        Intent intent = new Intent(this, NoteEditor.class);
        intent.putExtra("op", Operation.EDIT);
        intent.putExtra("id", noteId);
        startActivity(intent);
    }

}
